extends RefCounted
class_name CemeteryMgr
##与状态池的未激活池相似，冻结并暂存单位

var cemetery:Dictionary[String,Array] = {}

func save_node(node:Node):
	var node_parent = node.get_parent()
	if node_parent:
		node_parent.call_deferred("remove_child",node)
	node.set_deferred("process_mode",node.PROCESS_MODE_DISABLED)
	node.hide()
	cemetery[node.scene_file_path] =  _create_tombstone(node.scene_file_path)
	cemetery[node.scene_file_path].append(node)

func _create_tombstone(node_path:String)->Array[Node]:
	if !cemetery.has(node_path):
		var tombstone:Array[Node] = []
		cemetery[node_path] = tombstone
	return cemetery[node_path]

func get_node(node:Node,permanent:bool = true)->Node:
	if permanent:
		erase_node(node)
	node.process_mode = node.PROCESS_MODE_INHERIT
	node.show()
	return node

func get_last_node(node_path:String,permanent:bool = false)->Node:
	var node:Node = cemetery[node_path].back()
	if permanent:
		erase_node(node)
	node.process_mode = node.PROCESS_MODE_INHERIT
	node.show()
	return node

func get_first_node(node_path:String,permanent:bool = false)->Node:
	var node:Node = cemetery[node_path].front()
	if permanent:
		erase_node(node)
	node.process_mode = node.PROCESS_MODE_INHERIT
	node.show()
	return node

func get_random_node(node_path:String,permanent:bool = false)->Node:
	var node:Node = cemetery[node_path].pick_random()
	if permanent:
		erase_node(node)
	node.process_mode = node.PROCESS_MODE_INHERIT
	node.show()
	return node

func erase_node(node:Node):
	cemetery[node.scene_file_path].erase(node)
